package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.base.datam.WiOrganB;
import com.ruoyi.common.core.utils.SpringUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.business.BusinessUtils;
import com.ruoyi.system.api.domain.SysDept;
import com.ruoyi.system.domain.SysRoleOrgan;
import com.ruoyi.system.domain.vo.TreeSelect;
import com.ruoyi.system.domain.vo.WiOrganBTreeSelect;
import com.ruoyi.system.mapper.SysRoleOrganMapper;
import com.ruoyi.system.mapper.WiOrganBMapper;
import com.ruoyi.system.service.ISysRoleOrganService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 角色和水管机构关联Service业务层处理
 * 
 * @author hwd
 * @date 2023-03-16
 */
@Service
public class SysRoleOrganServiceImpl implements ISysRoleOrganService
{
    @Autowired
    private SysRoleOrganMapper sysRoleOrganMapper;

    @Autowired
    private WiOrganBMapper wiOrganBMapper;

    /**
     * 查询角色和水管机构关联
     * 
     * @param roleId 角色和水管机构关联主键
     * @return 角色和水管机构关联
     */
    @Override
    public List<SysRoleOrgan> selectSysRoleOrganByRoleId(Long roleId)
    {
        return sysRoleOrganMapper.selectSysRoleOrganByRoleId(roleId);
    }

    /**
     * 查询角色和水管机构关联列表
     * 
     * @param sysRoleOrgan 角色和水管机构关联
     * @return 角色和水管机构关联
     */
    @Override
    public List<SysRoleOrgan> selectSysRoleOrganList(SysRoleOrgan sysRoleOrgan)
    {
        return sysRoleOrganMapper.selectSysRoleOrganList(sysRoleOrgan);
    }

    /**
     * 新增角色和水管机构关联
     * 
     * @param sysRoleOrgan 角色和水管机构关联
     * @return 结果
     */
    @Override
    public int insertSysRoleOrgan(SysRoleOrgan sysRoleOrgan)
    {
        return sysRoleOrganMapper.insertSysRoleOrgan(sysRoleOrgan);
    }

    /**
     * 修改角色和水管机构关联
     * 
     * @param sysRoleOrgan 角色和水管机构关联
     * @return 结果
     */
    @Override
    public int updateSysRoleOrgan(SysRoleOrgan sysRoleOrgan)
    {
        return sysRoleOrganMapper.updateSysRoleOrgan(sysRoleOrgan);
    }

    /**
     * 批量删除角色和水管机构关联
     * 
     * @param roleIds 需要删除的角色和水管机构关联主键
     * @return 结果
     */
    @Override
    public int deleteSysRoleOrganByRoleIds(Long[] roleIds)
    {
        return sysRoleOrganMapper.deleteSysRoleOrganByRoleIds(roleIds);
    }

    /**
     * 删除角色和水管机构关联信息
     * 
     * @param roleId 角色和水管机构关联主键
     * @return 结果
     */
    @Override
    public int deleteSysRoleOrganByRoleId(Long roleId)
    {
        return sysRoleOrganMapper.deleteSysRoleOrganByRoleId(roleId);
    }

    @Override
    public List<WiOrganBTreeSelect> selectWiOrganBTreeList(WiOrganB wiOrganB) {

        List<WiOrganB> wiOrganBS = treeList();
        return buildWiOrganBTreeSelect(wiOrganBS);
    }
    /**
     * 构建前端所需要下拉树结构
     *
     * @param wiOrganBS 部门列表
     * @return 下拉树结构列表
     */
    @Override
    public List<WiOrganBTreeSelect> buildWiOrganBTreeSelect(List<WiOrganB> wiOrganBS)
    {
        List<WiOrganB>wiOrganBTrees = buildWiOrganBTree(wiOrganBS);
        return wiOrganBTrees.stream().map(WiOrganBTreeSelect::new).collect(Collectors.toList());
    }
    /**
     * 构建前端所需要树结构
     *
     * @param wiOrganBS 水管机构列表
     * @return 树结构列表
     */
    @Override
    public List<WiOrganB> buildWiOrganBTree(List<WiOrganB> wiOrganBS)
    {
        List<WiOrganB> returnList = new ArrayList<WiOrganB>();
        List<String> tempList = wiOrganBS.stream().map(WiOrganB::getOrgCd).collect(Collectors.toList());
        for (WiOrganB wiOrganB : wiOrganBS)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(wiOrganB.getParentOrgCd()))
            {
                recursionFn(wiOrganBS, wiOrganB);
                returnList.add(wiOrganB);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = wiOrganBS;
        }
        return returnList;
    }

    /**
     * 获取树形机构列表
     *
     * @return
     */
    @Override
    public List<WiOrganB> treeList() {
        List<WiOrganB> wiOrganBS = wiOrganBMapper.selectWiOrganBList(null);
        String orgCdQuery;
        for (WiOrganB vo : wiOrganBS) {
            HashMap<String, Object> addvAndOrgCodeInfo = BusinessUtils.getAddvAndOrgCodeInfo(vo.getOrgCd());
            orgCdQuery = (addvAndOrgCodeInfo.get("resCode") + "").substring(0, (addvAndOrgCodeInfo.get("resCode") + "").length() - 2);
//            WiOrganB wiOrganB = wiOrganBMapper.selectWiOrganBByOrgCd((orgCdQuery + "000000").substring(0, 13));
            String orgCd = (orgCdQuery + "000000").substring(0, 13);
            List<WiOrganB> collect = wiOrganBS.parallelStream().filter(vo2 -> vo2.getOrgCd().equals(orgCd)).collect(Collectors.toList());
            if (collect.size() > 0 && collect.get(0) != null) {
                vo.setParentOrgCd(collect.get(0).getOrgCd());
            }
        }
        return wiOrganBS;
    }

    @Override
    public List<String> selectOrganCdListByRoleId(Long roleId) {
        List<SysRoleOrgan> sysRoleOrganList =sysRoleOrganMapper.selectSysRoleOrganByRoleId(roleId);
        List<String> orgCdList=new ArrayList<>();
        if (sysRoleOrganList.size()>0){
            for (SysRoleOrgan o:sysRoleOrganList
                 ) {
                orgCdList.add(o.getOrgCd());
            }
        }
        return orgCdList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<WiOrganB> list, WiOrganB t)
    {
        // 得到子节点列表
        List<WiOrganB> childList = getChildList(list, t);
        t.setChildren(childList);
        for (WiOrganB tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<WiOrganB> getChildList(List<WiOrganB> list, WiOrganB t)
    {
        List<WiOrganB> tlist = new ArrayList<WiOrganB>();
        Iterator<WiOrganB> it = list.iterator();
        while (it.hasNext())
        {
            WiOrganB n = (WiOrganB) it.next();
            if (StringUtils.isNotNull(n.getParentOrgCd()) && n.getParentOrgCd() == t.getOrgCd())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<WiOrganB> list, WiOrganB t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }
}
